package cn.duli.xianer.controller;

import cn.duli.xianer.pojo.GoodsPicture;
import cn.duli.xianer.pojo.MyResult;
import cn.duli.xianer.service.FileService;
import cn.duli.xianer.service.GoodsPictureService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/goodsPicture")
public class GoodsPictureController {

    @Resource
    private GoodsPictureService goodsPictureService;

    @Autowired
    FileService fileService;

    /**
     * 上传图片返回图片url
     * @param upFile
     * @return
     * @throws IOException
     */
    @RequestMapping("/saveImg")
    @RequiresRoles("business")
    public MyResult updateAvatar(@RequestParam(value = "file",required = false) MultipartFile upFile) throws IOException {
        String imgUrl = fileService.uploadOneFile(upFile);
        return new MyResult(200, "上传图片成功",null,null,imgUrl,null);
    }

    /**
     * 新增商品图片
     * @param
     * @return
     */
    @PostMapping(value = "/save")
    @RequiresRoles("business")
    public MyResult save(@RequestBody List<GoodsPicture> goodsPictureList) {
        for (int i=0;i<goodsPictureList.size();i++) {
            goodsPictureService.save(goodsPictureList.get(i));
        }
        return new MyResult(200, "新增商品图片集成功",null,null,null,null);
    }


    /**
     * 根据图片id删除图片
     * @param gpId
     */
    @RequestMapping("/deleteById/{gpId}")
    @RequiresRoles(logical = Logical.OR, value = {"admin", "business"})
    public MyResult deleteById(@PathVariable("gpId") Integer gpId) {
        goodsPictureService.deleteById(gpId);
        return new MyResult(200, "根据图片id删除图片",null,null,null,null);
    }

    /**
     * 根据商品id修改该商品下的所有图片信息（不使用）
     * @param goodsPictureList
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/update")
    @RequiresRoles("business")
    public MyResult update(@RequestBody List<GoodsPicture> goodsPictureList) throws IOException {
        for (int i=0;i<goodsPictureList.size();i++) {
            goodsPictureService.update(goodsPictureList.get(i));
        }
        return new MyResult(200, "根据商品id修改该商品下的所有图片信息成功",null,null,null,null);
    }

    /**
     * 根据图片id修改该图片信息
     * @param goodsPictureList
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/updateById")
    @RequiresRoles("business")
    public MyResult updateById(@RequestBody List<GoodsPicture> goodsPictureList) throws IOException {
        for (int i=0;i<goodsPictureList.size();i++) {
            // 已经新增的数据
            if (goodsPictureList.get(i).getGpId() != 0) {
                // 根据图片id修改图片
                goodsPictureService.updateById(goodsPictureList.get(i));
            }
        }
        return new MyResult(200, "根据图片id修改该图片信息",null,null,null,null);
    }


    /**
     * 修改主图
     * @param goodsPicture
     */
    @RequestMapping("/updateMainPic")
    @RequiresRoles("business")
    public MyResult updateMainPic(GoodsPicture goodsPicture) {
        goodsPictureService.updateMainPic(goodsPicture);
        return new MyResult(200, "修改主图成功",null,null,null,null);
    }

    /**
     * 根据商品id查询此商品下的所有图片
     * @param gpGid
     * @return
     */
    @GetMapping("/findByGoodsId/{gpGid}")
    public MyResult findByGoodsId(@PathVariable("gpGid") Integer gpGid) {
        List<GoodsPicture> list = goodsPictureService.findByGoodsId(gpGid);
        return new MyResult(200, "根据商品id查询此商品下的所有图片",null,list,null,null);
    }

    /**
     * 根据商品id查询此商品的主图
     * @param gpGid
     * @return
     */
    @GetMapping("/findMainPicByGoodsId/{gpGid}")
    public MyResult findMainPicByGoodsId(@PathVariable("gpGid") Integer gpGid) {
        GoodsPicture goodsPicture = goodsPictureService.findMainPicByGoodsId(gpGid);
        return new MyResult(200, "根据商品id查询此商品的主图",null,null,goodsPicture,null);
    }

    /**
     * 分页查询所有图片
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/findAll")
    @RequiresRoles("admin")
    public MyResult findAll(@RequestParam(defaultValue = "1") int pageNo,
                                   @RequestParam(defaultValue = "6") int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<GoodsPicture> pageInfo = new PageInfo<>(goodsPictureService.findAll());
        return new MyResult(200, "分页查询所有图片",null,null,null,pageInfo);
    }

    /**
     * 根据图片id批量删除图片
     * @param gpId
     */
    @RequestMapping("/batchDelete")
    @RequiresRoles("admin")
    public MyResult batchDelete(@RequestParam(value = "gpId") String gpId) {
        String[] gpIds = gpId.split(",");
        List<Integer> list = new ArrayList<>();
        for(String str:gpIds){
            list.add(Integer.parseInt(str));
        }
        goodsPictureService.batchDelete(list);
        return new MyResult(200, "根据图片id批量删除图片",null,null,null,null);
    }


    /**
     * 根据图片id查询该图片详情
     * @param gpId
     * @return
     */
    @GetMapping("/findById/{gpId}")
    public MyResult findById(@PathVariable("gpId") Integer gpId) {
        GoodsPicture goodsPicture = goodsPictureService.findById(gpId);
        return new MyResult(200, "根据图片id查询该图片详情",null,null,goodsPicture,null);
    }
}
